var hetong=({
	load : function (){
		$$.showPreloader("加载中");
		$.ajax({
			type : "post",
			dataType : 'json',
			url : "../contract/getContractListForAdmin.do",
			data : {
				userId : userId
			},
			success : function(data) {
				console.log(data);
				var html="";
				if(data.rows !=null&&data.rows.length>0){
					for(var i=0; i<data.rows.length; i++){
						var agree = data.rows[i];
						var status_="";
						if(agree.status=="-1"){
							status_="草稿";
						}else if(agree.status=="1"){
							status_="已签名";
						}else if(agree.status=="0"){
							status_="待签名";
						}
						var contractType=agree.contractType;
						var type="";
						if(contractType==1) type='精装空房托管';
						if(contractType==2) type='购置家居套餐托管';
						if(contractType==3) type='家私家电齐全托管';
						
						if(agree.status=="-1" || agree.status ==null){
						 html+="<li class=\"item-content\" >"+((agree.houseAddress==""?"":agree.houseAddress)+"合同");
						 html+=" <div class=\"item-after\" style='    float: right;'><p style=\"margin: 0em 0;\"><a onclick=\"hetong.sendHeTongMessage('"+agree.contractId+"','"+(agree.userName+(agree.houseAddress==""?"":agree.houseAddress)+"合同")+"','"+type+"','"+agree.userName+"','"+agree.userId+"','"+(agree.city+(agree.houseAddress==""?"":agree.houseAddress))+"','"+"见合同"+"')\" class=\"button button-success\">发送合同</a></p></div> </div>";
						 html+=" <div class='operate-btn'><span id='edit-btn' class='edit-btn' user-id="+agree.userId+" contract-id="+agree.contractId+" >修改</span><span id='del-btn' class='del-btn' contract-id="+agree.contractId+">删除</span></div></li>";
						}else if(agree.status=="1"){
							html+="<li class=\"item-content \" contract-id="+agree.contractId+">"+((agree.houseAddress==""?"":agree.houseAddress)+"合同");
							html+=" <div class=\"item-after\" style='    float: right;'><p style=\"margin: 0em 0;\"><img style='width: 30px;margin-left: 10px;vertical-align: -webkit-baseline-middle;' src='../images/sessage.png'></p></div> </div>";
							 html+=" <div class='operate-btn'><span id='edit-btn' class='edit-btn' user-id="+agree.userId+" contract-id="+agree.contractId+" >修改</span><span id='del-btn' class='del-btn' contract-id="+agree.contractId+">删除</span></div></li>";
						}
					}			

					$("#hetong").html(html);
					$(".edit-btn").click(function(){
						window.location.href = "contractCreate.html?userId=" + $(this).attr("user-id") + '&contractId=' + $(this).attr("contract-id");
					});
					$(".del-btn").click(function(){
						var contractid= $(this).attr("contract-id");
						 $$.confirm('确定删除合同?', '温馨提示', function () {
							 hetong.delContract(contractid);
					      });
					});
					$(".viewContract").click(function(){
						window.location.href = "contractTerms.html?&contractId=" +$(this).attr("contract-id");
					});
					
				}else{
					$("#hetong").html("<li  style=\'text-align:center;margin-top:15%;margin-bottom:10px;\'><img src=\"../images/nondata.png\"></li><li style=\'text-align:center;\'>暂无数据~</li>");
				}
				$$.hidePreloader();
			},
			error: function(data){
				$("#hetong").html("<li  style=\'text-align:center;margin-top:15%;margin-bottom:10px;\'><img src=\"../images/nondata.png\"></li><li style=\'text-align:center;\'>服务器出错，请稍后再试~</li>");
				$$.hidePreloader();
			}
		});
	},
	zukeload : function (){
		$$.showPreloader("加载中");
		$.ajax({
			type : "post",
			dataType : 'json',
			url : "../zukeContract/getContractListForAdmin.do",
			data : {
				userId : userId
			},
			success : function(data) {
				console.log(data);
				var html="";
				if(data.rows !=null&&data.rows.length>0){
					for(var i=0; i<data.rows.length; i++){
						var agree = data.rows[i];
						var status_="";
						if(agree.status=="-1"){
							status_="草稿";
						}else if(agree.status=="1"){
							status_="已签名";
						}else if(agree.status=="0"){
							status_="待签名";
						}
						var rental_type=agree.rental_type;
						var type="";
						if(rental_type==1) type='整租';
						if(rental_type==2) type='合租';
						
						if(agree.status=="-1" || agree.status ==null){
						 html+="<li class=\"item-content\" >"+((agree.renter_addr==""?"":agree.renter_addr)+"合同");
						 html+=" <div class=\"item-after\" style='    float: right;'><p style=\"margin: 0em 0;\"><a onclick=\"hetong.sendHeTongMessage('"+agree.contract_id+"','"+(agree.renter_name+(agree.renter_addr==""?"":agree.renter_addr)+"合同")+"','"+type+"','"+agree.renter_name+"','"+agree.userId+"','"+((agree.renter_addr==""?"":agree.renter_addr))+"','"+"见合同"+"')\" class=\"button button-success\">发送合同</a></p></div> </div>";
						 html+=" <div class='operate-btn'><span id='edit-btn' class='edit-btn' user-id="+agree.userId+" contract-id="+agree.contract_id+" >修改</span><span id='del-btn' class='del-btn' contract-id="+agree.contract_id+">删除</span></div></li>";
						}else if(agree.status=="1"){
							html+="<li class=\"item-content \" contract-id="+agree.contract_id+">"+((agree.renter_addr==""?"":agree.renter_addr)+"合同");
							html+=" <div class=\"item-after\" style='    float: right;'><p style=\"margin: 0em 0;\"><img style='width: 30px;margin-left: 10px;vertical-align: -webkit-baseline-middle;' src='../images/sessage.png'></p></div> </div>";
							 html+=" <div class='operate-btn'><span id='edit-btn' class='edit-btn' user-id="+agree.userId+" contract-id="+agree.contract_id+" >修改</span><span id='del-btn' class='del-btn' contract-id="+agree.contract_id+">删除</span></div></li>";
						}
					}			

					$("#hetong").html(html);
					$(".edit-btn").click(function(){
						if(agreementType==1){
							window.location.href = "contractCreate.html?userId=" + $(this).attr("user-id") + '&contractId=' + $(this).attr("contract-id");
						}else{
							window.location.href = "renterContractCreate.html?userId=" + $(this).attr("user-id") + '&contractId=' + $(this).attr("contract-id");
						}
						
					});
					$(".del-btn").click(function(){
						var contractid= $(this).attr("contract-id");
						 $$.confirm('确定删除合同?', '温馨提示', function () {
								if(agreementType==1){
									 hetong.delContract(contractid);
								}else{
									 hetong.delRenterContract(contractid);
								}
							
					      });
					});
					$(".viewContract").click(function(){
						window.location.href = "contractTerms.html?&contractId=" +$(this).attr("contract-id");
					});
					
				}else{
					$("#hetong").html("<li  style=\'text-align:center;margin-top:15%;margin-bottom:10px;\'><img src=\"../images/nondata.png\"></li><li style=\'text-align:center;\'>暂无数据~</li>");
				}
				$$.hidePreloader();
			},
			error: function(data){
				$("#hetong").html("<li  style=\'text-align:center;margin-top:15%;margin-bottom:10px;\'><img src=\"../images/nondata.png\"></li><li style=\'text-align:center;\'>服务器出错，请稍后再试~</li>");
				$$.hidePreloader();
			}
		});
	},
	sendHeTongMessage : function (contractId,agreementName,agreementType,signUserName,userId,houseName,price){
		event.stopPropagation();
		$.ajax({
			type : "POST",
			dataType : 'json',
			url : "../weixinImpl/sendHeTongMessage.do",
			data : {
				userId : userId, 
				templateId : "4llD4HhiNBuZi0eLq3_mVyRRl3C8AEkgjiEQyYxGWSE",
				title : contractId,
				agreementName : agreementName,
				agreementType: agreementType,
				price :price,
				floor :houseName,
				signUserName : signUserName ,
				remark:"谢谢合作"
			},
			success : function(data) {
				console.log(data);
				$$.toast(data.msg);
			}
		});
		
	},
	toLink : function (txt,tagrt) {
		document.location.href = "./" + txt;
	},
	addAgreeMent : function (){
		if(agreementType==1){
			document.location.href = "./contractCreate.html?userId="+userId;
		}else{
			document.location.href = "./renterContractCreate.html?userId="+userId;
		}
	},
	delContract : function (contractId){
   	  $.ajax({
			type : "POST",
			dataType : 'json',
			url : "../contract/delContract.do",
			data : {
				contractId : contractId
			},
			success : function(data) {
				if(data.success){
					$$.toast(data.msg);
					hetong.load();
				}
			}
		});
	},
	delRenterContract : function (contractId){
	   	  $.ajax({
				type : "POST",
				dataType : 'json',
				url : "../zukeContract/delContract.do",
				data : {
					contractId : contractId
				},
				success : function(data) {
					if(data.success){
						$$.toast(data.msg);
						hetong.zukeload();
					}
				}
			});
		},
		goback : function (){
			window.location.href="../jsp/userInfo_YeZhu.html?userId="+userId;
		},

});
$("#butler").click(function(){
	 window.location.href = "../jsp/login.html";
});

$("#toguanjia").click(function(){
	 window.location.href = "../jsp/GuanJiaList.html";
});
